package xyz.hubery.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import xyz.hubery.enums.CodeEnum;
import xyz.hubery.mapper.SpecificationMapper;
import xyz.hubery.mapper.SpecificationOptionMapper;
import xyz.hubery.model.BusException;
import xyz.hubery.pojo.Specification;
import xyz.hubery.pojo.SpecificationOption;
import xyz.hubery.pojo.SpecificationOptions;
import xyz.hubery.service.SpecificationService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@DubboService
@Transactional
public class SpecificationServiceImpl implements SpecificationService {
    @Autowired
    private SpecificationMapper specificationMapper;
    @Autowired
    private SpecificationOptionMapper specificationOptionMapper;

    /**
     * 新增规格
     * @param specification
     */
    @Override
    public void add(Specification specification) {
        specificationMapper.insert(specification);
    }

    /**
     * 修改规格
     * @param specification
     */
    @Override
    public void update(Specification specification) {
        specificationMapper.updateById(specification);
    }

    /**
     * 删除规格
     * @param ids
     */
    @Override
    public void delete(Long[] ids) {
        // 删除规格-规格项数据
        QueryWrapper<SpecificationOption> wrapper = new QueryWrapper<>();
        wrapper.in("specId", ids);
        specificationOptionMapper.delete( wrapper);
        // 删除规格
        specificationMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 根据id查询规格
     * @param id
     * @return
     */
    @Override
    public Specification findById(Long id) {
        // 查询规格
        Specification specification = specificationMapper.selectById(id);
        if(specification == null) {
            throw new BusException(CodeEnum.SPECIFICATION_NOT_EXIST);
        }
        // 查询规格项
        QueryWrapper<SpecificationOption> wrapper = new QueryWrapper<>();
        wrapper.eq("specId", id);
        List<SpecificationOption> specificationOptions = specificationOptionMapper.selectList(wrapper);
        specification.setSpecificationOptions(specificationOptions);
        return specification;
    }

    @Override
    public Page<Specification> search(int page, int size) {
        return specificationMapper.selectPage(new Page(page,size),null);
    }

    /**
     * 根据商品类型id查询规格
     * @param id
     * @return
     */
    @Override
    public List<Specification> findByProductTypeId(Long id) {
        QueryWrapper<Specification> wrapper = new QueryWrapper<>();
        wrapper.eq("productTypeId", id);
        // 查询规格
        List<Specification> specifications = specificationMapper.selectList(wrapper);
        // 循环查询规格项
        if(specifications != null) {
            for (Specification specification : specifications) {
                QueryWrapper<SpecificationOption> optionWrapper = new QueryWrapper<>();
                optionWrapper.eq("specId", specification.getId());
                List<SpecificationOption> specificationOptions = specificationOptionMapper.selectList(optionWrapper);
                specification.setSpecificationOptions(specificationOptions);
            }
        }
        return specifications;
    }

    /**
     * 新增规格项
     * @param specificationOptions
     */
    @Override
    public void addOption(SpecificationOptions specificationOptions) {
        for (String optionName : specificationOptions.getOptionName()) { // 循环规格项
            SpecificationOption option = new SpecificationOption();
            option.setOptionName(optionName);
            option.setSpecId(specificationOptions.getSpecId());
            specificationOptionMapper.insert(option);
        }
    }

    /**
     * 删除规格项
     * @param ids
     */
    @Override
    public void deleteOption(Long[] ids) {
        specificationOptionMapper.deleteBatchIds(Arrays.asList(ids));
    }
}
